#include <stdio.h>
#include "debug.h"
#include "esp_task.h"
#include "door_bell_sound.h"
#include "door_bell_wifi.h"
#include "door_bell_mqtt.h"
#include "door_bell_ws.h"
#include "door_bell_video.h"
#include "door_bell_led.h"

// Callback function for successful WiFi connection
void wifi_connect_succ(void)
{
    MY_LOGI("connection successfully");

    // Initialize MQTT when WiFi connection is successful
    door_bell_mqtt_Init();

    // Initialize WebSocket when WiFi connection is successful
    door_bell_ws_Init();
}

// Callback function for failed WiFi connection
void wifi_connect_fail(void)
{
    MY_LOGI("connection failure");
}

uint8_t data[1024]; // Buffer for audio data
TaskHandle_t mainTask_Handle; // Handle for the main task

// Main application entry point
void app_main(void)
{
    MY_LOGI("door_bell start");

    // Get the handle of the current task (main task)
    mainTask_Handle = xTaskGetCurrentTaskHandle();

    // Initialize sound module
    door_bell_sound_Init();

    // Open sound device
    door_bell_sound_Open();

    // Initialize camera
    door_bell_camera_init();

    // Configure LED
    door_bell_configure_led();

    // Initialize WiFi
    door_bell_wifi_Init();

    // Start WiFi connection with success and failure callbacks
    door_bell_wifi_start(wifi_connect_succ, wifi_connect_fail);

    uint32_t cmd = 0; // Variable to store received commands
    
    // Main application loop
    while (1)
    {
        // Audio processing example (commented out)
        // if (door_bell_sound_Read(data, sizeof(data)) == ESP_CODEC_DEV_OK)
        // {
        //     door_bell_sound_Write(data, sizeof(data));
        // }
        // Note about audio processing timing:
        // Sample rate is 16k/s, every sample has 16 bit(2 byte), 1 channel
        // 16000 * 2 * 1 = 32000 bytes/s
        // 1000ms/20ms = 50 reads per second
        // data[1024]: 1 read = 1KB, needs 32 reads per second to keep up
        // Delay of 20ms means 50 reads per second, which is sufficient
        // vTaskDelay(20 / portTICK_PERIOD_MS);

        // Wait for a notification/command
        xTaskNotifyWait(0, 0, &cmd, portMAX_DELAY);
        
        // Process the received command
        if (cmd == ON_AUDIO_MASTER)
        {
            MY_LOGI("on audio master");
            door_bell_ws_audio_open(audio_to_master); // Open audio to master
        }
        else if (cmd == ON_AUDIO_VISITOR)
        {
            MY_LOGI("on audio visitor");
            door_bell_ws_audio_open(audio_to_visiter); // Open audio to visitor
        }
        else if (cmd == ON_VIDEO)
        {
            MY_LOGI("on video");
            door_bell_ws_video_open(); // Open video stream
            led_on(); // Turn on LED
        }
        else if (cmd == OFF_AUDIO_MASTER)
        {
            MY_LOGI("off audio master");
            door_bell_ws_audio_close(audio_to_master); // Close audio to master
        }
        else if (cmd == OFF_AUDIO_VISITOR)
        {
            MY_LOGI("off audio visitor");
            door_bell_ws_audio_close(audio_to_visiter); // Close audio to visitor
        }
        else if (cmd == OFF_VIDEO)
        {
            MY_LOGI("off video");
            door_bell_ws_video_close(); // Close video stream
            led_off(); // Turn off LED
        }
    }
}